package com.hmall.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.io.entity.StringEntity;

import java.io.IOException;

public class CozeWorkflowClient {
    // 扣子 API 地址
    private static final String API_URL = "https://api.coze.cn/v1/workflow/run";
    // 替换为你的 Access Token
    private static final String ACCESS_TOKEN = "pat_aHp0HV3romMYSKUPuRamRsaXOVdL2H49nCU3toz6KI7t1acLoEVlgcM8a813dOjZ";
    // 替换为你的 Workflow ID
    private static final String WORKFLOW_ID = "7562430993706582054";

    public static void main(String[] args) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            // 1. 构建请求参数
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode requestBody = objectMapper.createObjectNode()
                    .put("workflow_id", WORKFLOW_ID)
                    .set("parameters", objectMapper.createObjectNode()
                            // 工作流开始节点的输入参数
                            .put("age", "18")
                            .put("hobby", "打篮球")
                            .put("mbti", "infp")
                            .put("nickname", "赴野")
                            .put("region", "福建")
                            .put("xingzuo", "双鱼座")
                    );

//            {
//                "age": "18",
//                    "hobby": "打篮球",
//                    "mbti": "infp",
//                    "nickname": "赴野",
//                    "region": "福建",
//                    "xingzuo": "双鱼座"
//            }
            // 2. 创建 HTTP POST 请求
            HttpPost httpPost = new HttpPost(API_URL);
            httpPost.setHeader("Authorization", "Bearer " + ACCESS_TOKEN);
            httpPost.setHeader("Content-Type", "application/json");
            httpPost.setEntity(new StringEntity(
                    objectMapper.writeValueAsString(requestBody),
                    ContentType.APPLICATION_JSON
            ));

            // 3. 发送请求并处理响应
            httpClient.execute(httpPost, response -> {
                if (response.getCode() == 200) {
                    JsonNode responseNode = objectMapper.readTree(response.getEntity().getContent());
                    if (responseNode.get("code").asInt() == 0) {
                        // 成功获取结果
                        String result = responseNode.get("data").asText();
                        String debugUrl = responseNode.get("debug_url").asText();
                        System.out.println("工作流执行结果: " + result);
                        System.out.println("调试链接: " + debugUrl);
                    } else {
                        System.err.println("API 调用失败: " + responseNode.get("msg").asText());
                    }
                } else {
                    System.err.println("HTTP 请求失败，状态码: " + response.getCode());
                }
                return null;
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}